var useable = true;
//for payment button in single page
function SinglePage()
{
	//Newss Responsibility
	var Tabs = getCurrentTabData();
	Tabs.page = "Payment";
	waitingDialog({});
	if(Tabs.Payment == null)
		loadPaymentService();
}

function backToDetailPage()
{
	//Newss Responsibility
	var Tabs = getCurrentTabData();
	Tabs.page = "Detail";
	DisplayDetail();
}
//Prepare Service
function loadPaymentService(){
	var Tabs = getCurrentTabData();
	var services = [];
	if(SharedData.PayModeAndPayType==null) services.push(["/paytypeService",{func:"GetAllPayModeAndPayType"},function(data,args){SharedData.PayModeAndPayType = JSON.parse(data);},null]);
	services.push(["/folioService?",{func:"loadSingleFolioList",resId:Tabs.resId},load_folio_success,Tabs]);
	arrayservice(services,DisplaySinglePaymentPage,Tabs);
}

//Display
function DisplaySinglePaymentPage(){
	//Newss Responsibility
	CheckResBeforePayment();
	closeWaitingDialog();
	DisplayDetail();
}
function DisplayPaymentDetail()
{
	//Newss Responsibility
	$("#single-payment-dialog").css("display","");
	$("#single-payment-dialog").css("position","absolute");
	$("#single-payment-dialog").css("z-index","100001");
	fillDataInSinglePayment();
	DisplayPaymentData();
}

function load_folio_success(data,Tabs){
	Tabs.FolioList = JSON.parse(data);
}
function fillDataInSinglePayment(){
	fillPaymentMode();
	fillPaymentOwner();
	fillPaymentTitle();
	//fillaccountStateMentSingle()
	//LoadSinglePaymentPage(id,type);
	//accountStateMentSingle(id);
	//ShowSingleFolio(id,domData);
}

function DisplayPaymentData()
{
	var Tabs = getCurrentTabData();
	fuseCharge();
	fillRoomDetailInAccountStateMent();
	fillChargeInAccountStatement();
	fillPaymentInAccountStateMent();
	fillFolio();
	fillAllcost();
	if(Tabs.folioInPaymentPage != null)
	{
		var folioId= Tabs.folioInPaymentPage;
		var type= Tabs.folioPaymentType;
		LoadFolioDataInPayment(folioId,type,Tabs);
	}
	showPaymentcommandbutton();
}

function fuseCharge()
{
	
	var Tabs = getCurrentTabData();
	if(Tabs.OtherChargeList!=null)
	for(var i= 0;i<Tabs.OtherChargeList.length;i++)
		Tabs.ChargeList.push(Tabs.OtherChargeList[i]);
}

function fillPaymentOwner(){
	var Tabs = getCurrentTabData();
	if(Tabs.type == "Single"){
		(Tabs.calBalance == null)?CalBalancetoShow():"";
		var name = (Tabs.Owner != null && Tabs.Owner.firstName != null)? Tabs.Owner.firstName : "-";
		name += (Tabs.Owner != null && Tabs.Owner.lastName!=null)? " "+Tabs.Owner.lastName : "";
		$('table[name=bookingDetail] tr[displaytype=group]').css("display","none");
		$('table[name=bookingDetail] tr[displaytype=single]').css("display","");
		$('table[name=bookingDetail] span[name=bookingId]').text(booking_detail_label);
		$('table[name=bookingDetail] span[name=owner]').text(name);
		$('table[name=bookingDetail] span[name=address]').text((Tabs.Owner!= null)?Tabs.Owner.addressLine1:"");
		$('table[name=bookingDetail] span[name=tel]').text((Tabs.Owner!= null)?Tabs.Owner.phone:"");
		var roomTypeString = "";
		var roomType = getListOfFieldId(Tabs.SplitTable.Object,"roomTypeId");
		for(var i=0;i<roomType.length;i++)
		{
			var rooms = getListFromDataIndex(Tabs.SplitTable.Object,"roomTypeId",roomType[i]);
			roomTypeString += "," + rooms[0].roomTypeShortName + "(";
			for(var j=0;j<rooms.length;j++)
				roomTypeString += rooms[j].roomName + ",";
			roomTypeString = roomTypeString.substring(1,roomTypeString.length-1) + ")";
		}
		$('table[name=bookingDetail] div[name=roomtype]').text(roomTypeString);
		$('table[name=bookingDetail] div[name=checkInDate]').text(
			new Date(Tabs.ResData.date).getFormatedString() + " - " + new Date(Tabs.ResData.date).addDate(Tabs.ResData.span).getFormatedString() +
			"("+ Tabs.ResData.span +" "+ simpleLabel_Night +")");
		$('table[name=bookingDetail] div[name=pricerate]').text(Tabs.ResData.priceRateName);
		$('table[name=bookingDetail] div[name=roomamount]').text(1);
//		$('table[name=bookingDetail] div[name=adult]')
//		$('table[name=bookingDetail] div[name=child]')
		$('[name=pricebalance]:visible').html(toCurrencyString((Tabs.calBalance.TotalBalance+(Tabs.calBalance.otherTax+Tabs.calBalance.roomsTax))-Tabs.calBalance.payprice,false));
	}
}
function fillPaymentTitle(){
	var Tabs = getCurrentTabData();
	if(Tabs.fromGroupId==null)
		$('[displaytype=group]').css("display","none");
	else
		$('[displaytype=group]').css("display","");
	$('[displaytype=single]').css("display","");
	var name = (Tabs.Owner != null && Tabs.Owner.firstName != null)? Tabs.Owner.firstName : "-";
	name += (Tabs.Owner != null && Tabs.Owner.lastName!=null)? Tabs.Owner.lastName : "";
	if(Tabs.ResData.isAssign) name += " (A)";
	$("#single-payment-ingroup-info [name=guestname]").text(name);
	$("#single-payment-ingroup-info [name=agent]").text((Tabs.Agency.agency == '')? "-" : Tabs.Agency.agency);
	$("#single-payment-ingroup-info [name=resId]").text(Tabs.resId);
	$("#single-payment-ingroup-info [name=groupcode]").text((Tabs.fromGroupId!=null)? Tabs.fromGroupId : "");
	$("#single-payment-ingroup-info [name=resstatus]").text(getLocalizeResvStatus(Tabs.ResData.reservationStatusName));
	$("#single-payment-ingroup-info [name=roomtype]").text(Tabs.ResData.roomTypeName);
	$("#single-payment-ingroup-info [name=roomName]").text(Tabs.ResData.roomName);
}

function fillRoomDetailInAccountStateMent()
{
	var Tabs = getCurrentTabData();
	var DetailResStateMent = Tabs.ResData.roomTypeName+" "+Tabs.ResData.roomName
		+"/"+new Date(Tabs.ResData.date).getFormatedDateAndMonthString() + " - " 
		+ new Date(Tabs.ResData.date).addDate(Tabs.ResData.span).getFormatedDateAndMonthString()+"/"
		+Tabs.resId;
	var datelill = Tabs.ResData.dateCheckin;
	if(datelill == null)
		datelill = Tabs.ResData.date;
	$("#foliogroup_singlepayment_footer_btn").show();
	$("#back_to_folio_btn").hide();
	$("#innerfoliogroup_singlepayment_footer_btn").hide();
	$('[name*=StateMentListSingle]').remove();
	var arrayDetailResList = ('<tr name="StateMentListSingle" class="payment-hover-roompayment-unhover" id ="Single'+Tabs.resId+'">'+
		'<td  reservationId ="'+Tabs.resId+'"  name="resrooms">'+
		'<span>'+new Date(datelill).getFormatedString()
		+"/"+new Date(datelill).getHours()+":"
		+new Date(datelill).getMinutes()+'</span>'+
		'</td>'+
		'<td><span>'+DetailResStateMent+'</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'</tr>');
	if($("[id*=Single"+Tabs.resId+"]").size() ==0)
		$('#paymentListGroupSingle').before(arrayDetailResList);
}

function fillPaymentInAccountStateMent(paymentData)
{
	var Tabs = getCurrentTabData();
	var paymentList = Tabs.PaymentList;
	if(paymentData!=null)
	{
		$('[name*=checkbox_payment_res]').parents("tr").remove();
		if(paymentData.length==0)
			return;
		paymentList=paymentData;
	}
	if(paymentList == null)
		return;
	var arrayPaymentList ="";
	for(var i =0;i<paymentList.length;i++)
	{
		 arrayPaymentList = ('<tr name="StateMentListSingle" class="SinglepaymentList'+Tabs.resId+'" style="background-color:#F7F9A2">'+
					'<td>'+
					'<span><input resId ="Single'+Tabs.resId+'"  value="'+paymentList[i].id+'" type="checkbox" name="checkbox_payment_resSingle"></span><span>'+new Date(paymentList[i].payDate).getFormatedString()+'</span>'+
					'</td>'+
					'<td><span>'+paymentList[i].description+'</span></td>'+
					'<td><span style="color:#3b69b3;">&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;">&nbsp;</span></td>'+
					'<td><span style="float:right;">0.00</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;">'+parseFloat(paymentList[i].amount).toFixed(currencyDecimalPlaces)+'</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'</tr>');
		 if($("[value*="+paymentList[i].id+"]").parents("tr").size()==0){$('#paymentListGroupSingle').after(arrayPaymentList);}
	}
}

function fillChargeInAccountStatement(ChargeData)
{
	var Tabs = getCurrentTabData();
	var chargeList = Tabs.ChargeList;
	var arrayChargeList ="";
	$('[name*=Singlecheckbox_charge_res]').parents("tr").remove();
	if(ChargeData!=null)
	{
		if(ChargeData.length==0)
			return;
		chargeList=ChargeData;
	}
	if(chargeList == null)
		return;
	for(var i =0;i<chargeList.length;i++)
	{
		var disPlayFolioStatus="";
		if(!chargeList[i].isTemp){
			var folioNumber="";
			var q ="";
			var rc="";
			if(chargeList[i].Qty != parseInt(0))
			{
				q=Qty+" "+chargeList[i].Qty+" ";
				rc=roomCharge+"/";
			}
			if(chargeList[i].FolioId !=0)
			{
				disPlayFolioStatus="PI";
				folioNumber = chargeList[i].FolioId;
			}
			arrayChargeList = ('<tr name="StateMentListSingle" class=SinglechargeList'+Tabs.resId+' >'+
				'<td>'+
				'<span><input resId = "'+Tabs.resId+'" value="'+chargeList[i].id+'" folioId='+chargeList[i].FolioId+' cost="'+parseFloat(+chargeList[i].chargeAmount).toFixed(currencyDecimalPlaces)+'" type="checkbox" name=Singlecheckbox_charge_res'+i+' ></span><span>'
				+new Date(chargeList[i].chargeDate).getFormatedString()+'</span>'+
				'</td>'+
				'<td><span>'+q+'</span><span name="roomcharge_refund">'+rc+'</span><span id=description'+i+'>'+chargeList[i].description+'</span></td>'+
				'<td><span style="color:#3b69b3;" name="folio_number_onshow">'+disPlayFolioStatus+" "+folioNumber+'</span></td>'+
				'<td><span>&nbsp;</span></td>'+
				'<td><span id=charge'+i+' charge="'+chargeList[i].chargeAmount+'">'+toCurrencyString(chargeList[i].chargeAmount,false)+'</span></td>'+
				'<td><span id=tax'+i+' tax="'+chargeList[i].tax+'" >'+toCurrencyString(chargeList[i].tax,false)+'</span></td>'+
				'<td><span>&nbsp;</span></td>'+
				'<td><span>&nbsp;</span></td>'+
				'</tr>');
				if($("[value*="+chargeList[i].id+"]").parents("tr").size()==0){
					if(chargeList[i].isRefund)
					{
						var chargeText = $(arrayChargeList);
						var checkboxItem= chargeText.children(":first").find("span:first input");
						checkboxItem.attr("disabled", true);
						var refundText= chargeText.clone();
						refundText.find("[name=roomcharge_refund]").text(refundCharge+"/");
						refundText.children(":first").find("span:first input").removeAttr("name");
						$('#chargeListGroupSingle').after('<tr name="StateMentListSingle" class=SinglechargeList'+Tabs.resId+'>'
						+chargeText.html()
						+'</tr>'
						+'<tr name="StateMentListSingle" class="isrefund" >'+refundText.html()+'</tr>');
					}
					else
						$('#chargeListGroupSingle').after(arrayChargeList);
				}
		}
	}
}

function fillAllcost()
{ 
	var Tabs = getCurrentTabData();
	$('#singlepayment-account-cost').text('');
	$('#singlepayment-account-tax').text('');
	$('#singlepayment-account-currency').text('');
	$('#singlepayment-account-payment').text('');
	$('[id=roomCharge]').text(0);
	$('#otherCharge').text(0);
    $('#taxCharge').text(0);
	$('#disCount').text(0);
	$('#totalTaxCharge').text(0);
	$('#totalPay').text(0);
	$('#totalBalance').text(0);
	if(Tabs.calBalance == null) return;
	if(Tabs.type == "Single"){
		$('#singlepayment-account-cost').html(toCurrencyString(Tabs.calBalance.chargePriceAcc,false));
		$('#singlepayment-account-tax').html(toCurrencyString(Tabs.calBalance.roomsTaxAcc,false));
		$('#singlepayment-account-currency').html(SharedData.General.defaultCurrency.symbol);
		$('#singlepayment-account-payment').html(toCurrencyString(Tabs.calBalance.payprice,false));
	}else{
		$('[id=payment-account-cost]').html(toCurrencyString(Tabs.calBalance.chargePriceAcc,false));
		$('[id=payment-account-tax]').html(toCurrencyString(Tabs.calBalance.roomsTaxAcc,false));
		$('[id=payment-account-currency]').html(SharedData.General.defaultCurrency.symbol);
		$('[id=payment-account-payment]').html(toCurrencyString(Tabs.calBalance.payprice,false));
	}
	$('[id=roomCharge]').html(toCurrencyString(Tabs.calBalance.roomsPrice,false));
	$('[id=otherCharge]').html(toCurrencyString(Tabs.calBalance.otherCharge,false));
    $('[id=taxCharge]').html(toCurrencyString(Tabs.calBalance.otherTax+Tabs.calBalance.roomsTax,false));
	$('[id=disCount]').html(toCurrencyString(Tabs.calBalance.otherdiscount+Tabs.calBalance.roomdiscount,false));
	
	var totalAmount = (Tabs.calBalance.roomsPrice+Tabs.calBalance.otherCharge);
	$('[id=totalTaxCharge]').html(toCurrencyString(totalAmount+(Tabs.calBalance.otherTax+Tabs.calBalance.roomsTax),false));
	$('[id=totalPay]').html(toCurrencyString(Tabs.calBalance.payprice,false));
	$('[id=totalBalance]').html(toCurrencyString((totalAmount+(Tabs.calBalance.otherTax+Tabs.calBalance.roomsTax))-Tabs.calBalance.payprice,false));
}

function CreateFolioTableData(resId,FolioId)
{
	if(FolioId == null || FolioId =="0")
	{
		simpleDialog("checked folio again!!"+FolioId);
		return;
	}
	var link = "/folioService?func=createFolioTable&folioId="+FolioId;
	simpleservice(link,null,function(data){
		var objArray = JSON.parse(data);
		if(objArray!=null){
			var arrayPaymentList="";
			var arrayChargeList="";
			var costoffSum=0;
			var costsum=0;
			var taxsum=0;
			var paysum=0;
			for(var i=0;i<objArray[0].length;i++)
			{
				arrayChargeList +="<tr name=\"StateMentListSingle\" class=\"SinglechargeList"+resId+"\" >"+
					'<td><span><input resId = "Single'+resId+'" value="'+objArray[0][i].id+'" type="checkbox" name=Singlecheckbox_charge_res'+i+' disabled></span><span>'+formatdateShow(objArray[0][i].chargeDate)+'</span></td>'+
					'<td><span id=descriptionSingle'+i+'>'+objArray[0][i].description+'</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;" id=chargeSingle'+i+'>'+parseFloat(objArray[0][i].chargeAmount).toFixed(currencyDecimalPlaces)+'</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'<td><span style="float:right;" id=taxSingle'+i+' >'+parseFloat(objArray[0][i].tax).toFixed(currencyDecimalPlaces)+'</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
				'</tr>';
				costsum+=parseFloat(objArray[0][i].chargeAmount);
				taxsum+=parseFloat(objArray[0][i].tax);
			}
			for(var i=0;i<objArray[1].length;i++)
			{
				arrayPaymentList +='<tr name="StateMentListSingle" style="background-color:#F7F9A2">'+
				'<td><span><input resId ="Single'+resId+'"  value="'+objArray[1][i].id+'" type="checkbox" name="checkbox_payment_resSingle"></span><span>'+formatdateShow(objArray[1][i].payTime)+'</span></td>'+
				'<td><span >'+objArray[1][i].Description+'</span></td>'+
				'<td><span style="color:#3b69b3;">&nbsp;</span></td>'+
				'<td><span>&nbsp;</span></td>'+
				'<td><span style="float:right;">&nbsp;</span></td>'+
				'<td><span style="float:right;">0.00</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
				'<td><span>&nbsp;</span></td>'+
				'<td><span style="float:right;">'+parseFloat(objArray[1][i].amount).toFixed(currencyDecimalPlaces)+'</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
				'</tr>';
				paysum+=parseFloat(objArray[1][i].amount);
			}
			$("#singlepayment-account-cost").text(parseFloat(costsum).toFixed(currencyDecimalPlaces));
			$("#singlepayment-account-tax").text(parseFloat(taxsum).toFixed(currencyDecimalPlaces));
			$("#singlepayment-account-currency").text(parseFloat(costoffSum).toFixed(currencyDecimalPlaces));
			$("#singlepayment-account-payment").text(parseFloat(paysum).toFixed(currencyDecimalPlaces));
			$("[name=StateMentListSingle]").not($("[name=StateMentListSingle][id]")).remove();
			$("[name=StateMentListSingle][id]").attr("id","Single"+resId);
			//$("input[type=checkbox][resid*="+resId+"][value][name*=Singlecheckbox_charge_res]").parent().parent().parent().remove();
		if(resId!= null)
			$(".payment-hover-roompayment[id*="+resId+"]").after(arrayPaymentList+arrayChargeList);
		else
		{
			$("#paymentListGroupSingle").replaceWith(arrayPaymentList);
			$("#chargeListGroupSingle").replaceWith(arrayChargeList);
		}
		}
	});
}

function LoadSinglePaymentPage(gerResId,type)
{
	if(gerResId !="0"||gerResId != null){
		var link = "/reservationService?func=loadBookingDetail&reservedId=" + gerResId +"&gId=0";
		simpleservice(link,null,function(data){
			var objArray = JSON.parse(data);
			if(type=="singlePaymentFolio")
				$("#single_Payment_bookingdetails_guest").text(objArray['groupOwner']);
			else 
			{
				$("#single_Payment_ingroup_bookingdetails_owner").text(objArray['groupOwner']);
			}
			$("#single-payment-info #single-payment-info-group_code").text(objArray['groupId']);
			$("#single-payment-info #single-payment-info-res_code").text(objArray['resId']);
			$("#single-payment-info #single-payment-info-resstatus").text(objArray['resStatus']);
			$("#single-payment-info #single-payment-info-roomtype").text(objArray['roomType']);
			$("#single-payment-info #single-payment-info-room").text(objArray['roomname']);
			$("#single-payment-info-group_code").text(objArray['groupId']);
			$("#single-payment-info-res_code").text(objArray['resId']);
			$("#single-payment-info-resstatus").text(objArray['resStatus']);
			$("#single-payment-info-roomtype").text(objArray['roomType']);
			$("#single-payment-info-room").text(objArray['roomname']);
		});
	}
}

function fillFolio(folioOjb)
{
	var text="";
	var Tabs = getCurrentTabData();
	var objFolio =Tabs.FolioList;
	if(folioOjb != null)
		objFolio=folioOjb;
	var textOwnerPayment="";
	var textOwnerCharge="";
	var folioCheck =null;
	$("[id=payment-singlefolio-list]").show();
	$("[id*=payment-folio-master-chekbox]").remove();
	$("[id=route-payment-to]").children().remove();
	$("[id*=route-new-folio-item]").find($("option[name]")).remove();
	$("[id*=route-to-folio-item]").find($("div[folioId]")).remove();
	if(objFolio != null)
	{
		$("#lender-singlefolio-list_0").attr("id","lender-singlefolio-list");
		$("[id*=lender-singlefolio-list_]").remove();
		$("#payment-singlefolio-list-footer").css("display","");
		$("#payment-singlefolio-list-generate-foliochb").css("display","");
		$("#payment-no-folio-list").hide();
		$("#payment-singlefolio-title").replaceWith(
		"<tr id =\"payment-singlefolio-title\">"+
		"<td colspan=\"1\" width=\"20%\"><input type=\"checkbox\" id=\"payment-folio-master-chekbox\" onclick=\"Allcheck()\"></td>"+
		"<td colspan=\"2\" width=\"45%\">"+guestLag+"</td>"+
		"<td colspan=\"1\" width=\"13%\">"+amountLag+"</td>"+
		"<td colspan=\"1\" width=\"6%\">"+paidLag+"</td>"+
		"<td colspan=\"1\" width=\"11%\">"+balanceLag+"</td>"+
		"<td colspan=\"1\" width=\"5%\">&nbsp;</td>"+
		"</tr>");
		for(var y=0;y<objFolio.length;y++)
		{
			for(var n=0;n<objFolio[y][1].length;n++)
			{
				text+="<tr id=\"lender-singlefolio-list_"+n+"\">"+
					"<td colspan=\"7\" width=\"100%\" style=\"background-color:#8F8FE7; padding-left:10px;\">"+
						"<strong><span>"+(n+1)+" </span><span>"+roomtypeLag+":</span>"+
						"<span id=\"payment-folio-roomtype\" style=\"color:#103083;\">"+objFolio[y][1][n]['roomType']+"</span>"+
						"<span id=\"payment-folio-roomname\" style=\"color:#103083;\">("+objFolio[y][1][n]['roomName']+")</span></strong>"+
					"</td>"+
				"</tr>";
				if($("#route-new-folio-item").find($("option[name]")).size()<objFolio[y][1].length)
					 $("#route-new-folio-item").append( $("<option name=\"resId\">").val(Tabs.resId).html(objFolio[y][1][n]['roomType']+objFolio[y][1][n]['roomName']));	
			
				for(var i=0;i<objFolio[y][0].length;i++)
				{
					var paids =objFolio[y][0][i]['paid'];
					if(paids == 0 || paids==null )
						paids ="-";
					var isLockStatus="<span class=\"iconlockedpocition iconlockandunlock\"></span>";
					if(!objFolio[y][0][i]['isLock'])
						;//TODO: fill class lock
					if(objFolio[y][0][i]['folioId'] == folioCheck)
					{
						text+="<tr style=\"background-color:#FFF;\">"+
						"<td colspan=\"3\" >"+
							"<table width=\"100%\" style=\"color:#FFF;\" ><tr>"+
								"<td colspan=\"1\" width=\"20%\"><input type=\"checkbox\" name=\"payment-folio-chekbox-res\" resId=\""+objFolio[y][0][i]['resId']+"\" folioId=\""+objFolio[y][0][i]['folioId']+"\"><span id=\"payment-folio-subfolio\" style=\"color:#103083; padding-left:20px;\">"+"PI "+objFolio[y][0][i]['folioId']+"</span></td>"+
								"<td colspan=\"2\" width=\"45%\"><span id=\"payment-folio-guestname\" style=\"color:#999\">"+objFolio[y][0][i]['guestName']+"</span>&nbsp;<span id=\"payment-folio-guestid\" style=\"color:#999\">"+"(P"+objFolio[y][0][i]['guestId']+")"+"</span></td>"+
								"<td colspan=\"1\" width=\"13%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span name=\"payment-folio-money-amount\" style=\"color:#999\">"+objFolio[y][0][i]['money']+"</span></td>"+
								"<td colspan=\"1\" width=\"6%\"><span id=\"payment-folio-paid\" style=\"color:#999\">"+paids+"</span></td>"+
								"<td colspan=\"1\" width=\"11%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span id=\"payment-folio-balance\" style=\"color:#999\">"+objFolio[y][0][i]['balance']+"</span></td>"+
								"<td colspan=\"1\" width=\"5%\">"+isLockStatus+"</td>"+
								"</tr></table>"+
						"</td></tr>";
						$("#payment-singlefolio-btn-total-amount").text(objFolio[y][0][i]['money']);
						$("#payment-singlefolio-total-paid").text(paids);
						$("#payment-singlefolio-total-balance").text(objFolio[y][0][i]['balance']);
					}
					else if(folioCheck == null)
					{
						var icon="";
						(objFolio[y][0][i]['isLock'])? icon=" iconlockedpocition":icon =" iconunlockpocition";
						var type = (objFolio[y][0][n]['checkIngroup'])? "singlePaymentFolioIngroup": "singlePaymentFolio";
						text+="<tr style=\"background-color:#FFF;\">"+
							"<td colspan=\"3\" >"+
								"<table width=\"100%\" style=\"color:#FFF;\" >"+
								"<tr class=\"payment-hover-folioitem\" type=\""+type+"\" onclick=\"if (useable) LoadFolioPage(this)\" >"+
									"<td colspan=\"1\" width=\"20%\"><input type=\"checkbox\" name=\"payment-folio-chekbox-res\" resId=\""+objFolio[y][0][i]['resId']+"\" folioId=\""+objFolio[y][0][i]['folioId']+"\" onmouseover=\"useable=false;\" onmouseout=\"useable=true;\">"+
										"<span id=\"payment-folio-subfolio\" style=\"color:#103083; padding-left:20px;\">"+"PI "+objFolio[y][0][i]['folioId']+"</span></td>"+
									"<td colspan=\"2\" width=\"45%\"><span id=\"payment-folio-guestname\" style=\"color:#999\">"+objFolio[y][0][i]['guestName']+"</span>&nbsp;<span id=\"payment-folio-guestid\" style=\"color:#999\">"+"(P"+objFolio[y][0][i]['guestId']+")"+"</span></td>"+
									"<td colspan=\"1\" width=\"13%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span name=\"payment-folio-money-amount\" style=\"color:#999\">"+objFolio[y][0][i]['money']+"</span></td>"+
									"<td colspan=\"1\" width=\"6%\"><span id=\"payment-folio-paid\" style=\"color:#999\">"+paids+"</span></td>"+
									"<td colspan=\"1\" width=\"11%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span id=\"payment-folio-balance\" style=\"color:#999\">"+objFolio[y][0][i]['balance']+"</span></td>"+
									"<td colspan=\"1\" width=\"5%\"><div class=\"iconlockandunlock "+icon+"\" onclick=\"lockFolio(this)\" islong="+objFolio[y][0][i]['isLock']+"></div></td>"+
								"</tr>"+
								"</table>"+
							"</td>"+
						"</tr>";
						$("#payment-singlefolio-btn-total-amount").text(objFolio[y][0][i]['money']);
						$("#payment-singlefolio-total-paid").text(paids);
						$("#payment-singlefolio-total-balance").text(objFolio[y][0][i]['balance']);
					}
					textOwnerPayment+="<div resid=\""+objFolio[y][0][i]['resId']+"\" folioId=\""+objFolio[y][0][i]['folioId']+"\" balance=\""+objFolio[y][0][i]['balance']+"\" class=\"route-payment-hover\" onclick=\"GetOwnerofPayment(this)\">"+
						"<span>"+objFolio[y][0][i]['roomType']+"("+objFolio[y][0][i]['roomName']+")("+objFolio[y][0][i]['folioId']+")"+"</span></div>";
					textOwnerCharge+="<div folioId=\""+objFolio[y][0][i]['folioId']+"\" balance=\""+objFolio[y][0][i]['balance']+"\" class=\"route-payment-hover\" onclick=\"GetFolioOfcharge(this)\">"+
						"<span>"+objFolio[y][0][i]['guestName']+" ("+objFolio[y][0][i]['folioId']+")"+"</span></div>";
				}
			}
		}
		$("input[name=payment-folio-chekbox-res]").parent().parent().parent().parent().parent().parent().remove();
		
		if($("#route-payment-to").children().size()<objFolio.length)
		{
			$("#route-payment-to").append(textOwnerPayment);
			$("#route-to-folio-item").append(textOwnerCharge);
		}
		textOwnerPayment="";
		textOwnerCharge="";
		$("#lender-singlefolio-list").replaceWith(text);
		text="";
		$("#payment-no-singlefolio-list").css("display","none");
	}else
	{
		$("#payment-singlefolio-title").replaceWith("<tr id =\"payment-singlefolio-title\"><td width=\"100%\">&nbsp;</td></tr>");
		$("[id*=lender-singlefolio-list]").hide();
		$("input[name=payment-folio-chekbox-res]").parent().parent().parent().parent().parent().parent().remove();
		$("#payment-singlefolio-list-footer").css("display","none");
		$("#payment-no-singlefolio-list").show();
	}
}

function ExceptTex()
{
	var exceptTexDialog='<div id="exceptTex_Dialog">'+exceptTex+'</div>'; 
	var buttons = [
	{title:btnrutechargeOk , callback: function(thisdialog,args){
			ExcaptTaxofCharge();
			thisdialog.dialog("close");
		}},
		{ title:btnCancel , callback: function(thisdialog,args){
			thisdialog.dialog("close");
		}
	}];
	customDialog(exceptTex,exceptTexDialog,{width:389},buttons,null);
}

function ExcaptTaxofCharge()
{
	if($("input[name*=checkbox_charge_res]:checked").size() != 0)
	{
		var chargeArray= new Array();
		$("input[name*=checkbox_charge_res]:checked").each(function(){
			chargeArray.push($(this).attr("value"));
		});	
		var url = "/chargeService?func=ExceptTex&chargeId="+chargeArray;
		simpleserviceWithArgs(url,null,ExceptTexSuccess,null);
	}
	else
		simpleDialog("select charge !!",100,null);
}

function ExceptTexSuccess(data,obj)
{
	var objArray = JSON.parse(data);
	if(JSON.parse(data) == null)
		simpleDialog(saveFail_label,100,null);
	else{
		var checkLogOut =  CheckSession(objArray);
		if(checkLogOut)
			return;
		simpleDialog("ExceptTexed !!",100,null);
	}
}

function LoadFolioPage(e)
{
	var folioId=$(e).children(":first").find("input:visible").attr("folioid");
	var type =$(e).attr("type");
	var Tabs = getCurrentTabData();
	Tabs.folioInPaymentPage =folioId;
	Tabs.folioPaymentType=type;
	var resCheck =$(e).children('td:first').find('input').attr('resId');
	Tabs.resCheck=resCheck;
	LoadFolioDataInPayment(Tabs.folioInPaymentPage,type,Tabs,resCheck);
}

function LoadFolioDataInPayment(folioId,type,Tabs,resCheck)
{
	var folioArray0=[];
	var folioArray1=[];
	var folioArray2=[];
	var folioArray22=[];
	var folioStatus="";
	var ChargeData=[];
	var paymentData=[];
	var folioAryList=Tabs.FolioList;
	var chargeAryList=Tabs.ChargeList;
	var paymentAryList=Tabs.PaymentList;
	if((type=="GroupSummaryFolio"&&resCheck!=null)||type == "singlePaymentFolioIngroup")
	{
		for(var i=0;i<Tabs.GroupResvList.length;i++)
		{
			if(Tabs.GroupResvList[i].resId == resCheck){
				var roomsResAry=[Tabs.GroupResvList[i]];
				folioAryList=Tabs.GroupResvList[i].FolioList;
				chargeAryList=Tabs.GroupResvList[i].ChargeList;
				paymentAryList=Tabs.GroupResvList[i].paymentList;
			}
		}
	}
	if((type=="GroupSummaryFolio"&&resCheck==null)||type == "paymentgroupFolio")
	{
		folioAryList=Tabs.FolioList;
		chargeAryList=Tabs.ChargeList;
		paymentAryList=Tabs.GroupPaymentList;
	}
	//folioData//
	for(var n = 0;n<folioAryList.length;n++)
		for(var i = 0;i<folioAryList[n][0].length;i++){
			var folioOjb ={};
			var roomDetailObj={};
			if(folioAryList[n][0][i].folioId == folioId )
			{
				folioOjb=folioAryList[n][0][i];
				folioArray2.push(folioOjb);
				folioArray1.push(folioArray2);
				for(var c = 0;c<folioAryList[n][1].length;c++){
					roomDetailObj =folioAryList[n][1][c];
					folioArray22.push(roomDetailObj);
					folioArray1.push(folioArray22);
				}
				folioArray0.push(folioArray1);
				folioStatus =FolioStatus(folioAryList[n][0][i].isLock);
			}
		}
	//ChargeData//
	if(chargeAryList!=null){
		for(var i= 0;i<chargeAryList.length;i++)
			if(chargeAryList[i].FolioId==folioId)
				ChargeData.push(chargeAryList[i]);
	}
	//PaymentData//
	if(paymentAryList!=null){
		for(var i= 0;i<paymentAryList.length;i++)
			if(paymentAryList[i].folioId==folioId)
				paymentData.push(paymentAryList[i]);
	}
	if($("[name*=folio_number_of_folio_page]").size()==0)
		$("[name*=bookingId]:visible").append("<span name=\"folio_number_of_folio_page\">("+folioStatus+" "+folioId+")</span>");
	if(type == "paymentgroupFolio")
	{
		$("[id=payment-folio-list]").show();
		$('[name=StateMentListSingle] [reservationid]').parents('tr').hide();
		$('[name*=bookingdetails]:visible').append('('+$('[id*=payment-folio-subfolio]').text()+')');
		ShowFolio(folioArray0);
		fillChargeInGroupAccountStatement(ChargeData);
		fillPaymentInGroupAccStateMent(paymentData,null);
		$("#back_to_folio_btn").hide();
		$("#innerfoliogroup_payment_footer_btn").show();
		$('[name=payment_footer_btn').not('[id]').hide();
		$('[name=Singlecheckbox_charge_resing]').parents('tr').remove();
		$('[name=checkbox_payment_res]').parents('tr').remove();
	}
	else if(type == "singlePaymentFolioIngroup")
	{
		$("[id=payment-folio-list]").show();
		$('[name=StateMentListSingle] [reservationid]').parents('tr').hide();
		ShowFolio(folioArray0);
		$('[name*=bookingdetails]:visible').append('('+$('[id*=payment-folio-subfolio]').text()+')');
		fillRoomDetailInGroupAccStateMent(roomsResAry);
		fillChargeInGroupAccountStatement(ChargeData);
		fillPaymentInGroupAccStateMent(null,paymentData);
		$('[name*=Singlecheckbox_charge_group]').parents('tr').remove();
		$('[name*=checkbox_payment_group]').parents('tr').remove();
		$('[name=StateMentListSingle][id]:visible').not('[id=Single'+Tabs.resCheck+']').remove();
		$("#back_to_folio_btn").hide();
		$("#innerfoliogroup_payment_footer_btn").show();
		$('[name=payment_footer_btn').not('[id]').hide();
	}
	else if(type == "singlePaymentFolio")
	{
		$("[id=payment-singlefolio-list]").show();
		fillFolio(folioArray0);
		fillChargeInAccountStatement(ChargeData);
		fillPaymentInAccountStateMent(paymentData);
		$("#back_to_folio_btn").hide();
		$("#innerfoliogroup_singlepayment_footer_btn").show();
		$("#foliogroup_singlepayment_footer_btn").hide();
	}else if(type=="singleSummaryFolio")
	{
		fillChargeInAccountStatement(ChargeData);
		fillPaymentInAccountStateMent(paymentData);
		$("[id=payment-singlefolio-list]:visible").hide();
		$("#innerfoliogroup_singlepayment_footer_btn").hide();
		$("#foliogroup_singlepayment_footer_btn").hide();
		$("#back_to_folio_btn").show();
	}else if(type=="GroupSummaryFolio")
	{
	fillChargeInGroupAccountStatement(ChargeData);
		if(resCheck != null){
			fillRoomDetailInGroupAccStateMent(roomsResAry);
			fillPaymentInGroupAccStateMent(null,paymentData);
			$('[name*=Singlecheckbox_charge_group]').parents('tr').remove();
			$('[name*=checkbox_payment_group]').parents('tr').remove();
			$('[name=StateMentListSingle][id]:visible').not('[id=Single'+Tabs.resCheck+']').remove();
		}else
		{
			$('[name=StateMentListSingle] [reservationid]').parents('tr').hide();
			fillPaymentInGroupAccStateMent(paymentData,null);
			$('[name=Singlecheckbox_charge_resing]').parents('tr').remove();
			$('[name=checkbox_payment_res]').parents('tr').remove();
		}
		
		$("[id=payment-folio-list]:visible").hide();
		$("#back_to_folio_btn").show();
		$("#innerfoliogroup_payment_footer_btn").hide();
		$('[name=payment_footer_btn').not('[id]').hide();
	}
	
	$("[name*=folio_number_onshow]:visible").remove();
}

function OpenFolioSummaryPage()
{
	var Tabs = getCurrentTabData();
	if(Tabs.type=="Single"){
		var folioId=Tabs.folioInPaymentPage;
		Tabs.folioInPaymentPage =folioId;
		Tabs.folioPaymentType="singleSummaryFolio";
		LoadFolioDataInPayment(folioId,Tabs.folioPaymentType,Tabs,null);
	}else
	{
		var folioId=Tabs.folioInPaymentPage;
		Tabs.folioInPaymentPage =folioId;
		Tabs.folioPaymentType="GroupSummaryFolio";
		LoadFolioDataInPayment(folioId,Tabs.folioPaymentType,Tabs,Tabs.resCheck);
	}
}

function BacktoAccountStatement()
{
	var Tabs = getCurrentTabData();
	if(Tabs.type=="Single"){
		Tabs.folioInPaymentPage =null;
		Tabs.folioPaymentType=null;
		fillFolio();
		fillChargeInAccountStatement();
		fillPaymentInAccountStateMent();
		$("#foliogroup_singlepayment_footer_btn").show();
		$("#innerfoliogroup_singlepayment_footer_btn").hide();
		$("[name*=folio_number_of_folio_page]").remove();
	}else
	{
		Tabs.folioInPaymentPage =null;
		Tabs.folioPaymentType=null;
		Tabs.resCheck=null;
		ShowFolio();
		fillRoomDetailInGroupAccStateMent();
		fillChargeInGroupAccountStatement(1,null);
		fillPaymentInGroupAccStateMent();
		$("#back_to_folio_btn").hide();
		$("#innerfoliogroup_payment_footer_btn").hide();
		$('[name=payment_footer_btn').not('[id]').show();
	}
}

function BacktoFolioPage()
{
	var Tabs = getCurrentTabData();
	if(Tabs.type=="Single"){
		$("[id=payment-singlefolio-list]").show();
		$("#innerfoliogroup_singlepayment_footer_btn").show();
	}else{
		$("[id=payment-folio-list]").show();
		$("#innerfoliogroup_payment_footer_btn").show();
	}
	$("#back_to_folio_btn").hide();
}

function FolioStatus(folioIslog)
{
	return (folioIslog)?"Inv":"PI";
}